function strRepeat(text, cnt) {
	var str = '';
	for (i=0; i < cnt; i++){
		str += text;
	}
	return str;
}

$(document).ready(function() {
	$('#favorite_link > a').bind('click', favorite);
	$('#favorite_items_wrapper').load(baseURI+'/admin/cp/ajax_load_favorites');
});


function favorite(activeDropGetter) {
	$('#favorite_link > a').blur();
	var oDialog = $('#favorite');
	
	if (activeDropGetter === true) {
		$('#item_getter').show();
		$('#empty_favorite').hide();
	}
	
	oDialog.css({
		left: ($('#favorite_link').offset().left-5)+'px',
		top: ($('#favorite_link').offset().top-7)+'px'
	});
	
	oDialog.show();
	
	$(document).bind('click',checkMouseOnFavorute);
		
	return false;
}

function checkMouseOnFavorute(e) {
	if (!$('#favorite').isMouseOn(e.pageX, e.pageY)) {
		hideFavorite();
		$(document).unbind('click',checkMouseOnFavorute);
	}
}

function hideFavorite() {
	$('#item_getter').hide();
	$('#favorite').hide();
}

function cpItemDropFavorite(event, elementId, cpItemId) {
	var itemId = cpItemId;
	var scode = scode;
	
	$('#'+elementId)
		.disableTextSelect()
		.drop(event, {
			target: $('#item_getter'),
			parent: $('#drag_wrapper_'+itemId),
			start: function(e, element, clone) {
				$('#item_getter').attr('getter_disabled');
				$(element).animate({opacity: 0.5}, 'fast');
				$(clone).css('background', '#fff');
				favorite(true);
			},
			end: function(e, element, clone) {
				if (!$('#favorite_items').children().size()) {
					$('#empty_favorite').show();
				}
				hideFavorite();
				$(element).animate({opacity: 1}, 'fast');
			},
			over: function() {
				$('#item_getter').attr('class','getter_enabled');
			},
			out: function() {
				$('#item_getter').attr('class','getter_disabled');
			},
			main: function(e, element, clone) {
				$(element).css('opacity', 1);
				$('#item_getter').hide();
				$('#empty_favorite').hide();
				
				$('#favorite_items').append(
					$('<div>')
						.attr('class','item')
						.attr('id', 'favorite_wrapper_'+itemId)
						.append(
							$(element)
								.clone()
								.attr('id', 'favorite_'+itemId)
						)
				);
				
				var scode = scode;
				
				ajax_add_favorite(itemId);
			}
			
		});
}

function ajax_add_favorite(id) {
	$.ajax({
		url: baseURI+'/admin/cp/ajax_add_favorite/'+id+'?scode='+scode,
		dataType: 'xml',
		type: 'get',
		cache: false,
		error: function() { alert ('Ошибка в процессе добавление избранного!')  },
		success: function(xml) {}
	});
}

function deleteFavorite(id, elementId) {
	$('#'+elementId).remove();
	
	$.ajax({
		url: baseURI+'/admin/cp/ajax_delete_favorite/'+id+'?scode='+scode,
		dataType: 'xml',
		type: 'get',
		cache: false,
		error: function() { alert ('Ошибка в процессе удаления!')  },
		success: function(xml) {}
	});
	
	if (!$('#favorite_items').children().size()) {
		$('#empty_favorite').show();
	}
}

/**
 * CP SETTINGS
 */
 
var b_treeLoaded = false;

function cpSettingsChangeIconField() {
	if ($('#cp_item_icon_path').css('display') == 'none') {
		$('#cp_item_icon_path').show();
		$('#cp_item_icon_file').hide();
	} else {
		$('#cp_item_icon_file').show();
		$('#cp_item_icon_path').hide();
	}
}

function cpSettingsSlide(cpDirId) {
	$('#list_cp_items > .item > .checkbox').toggle();
	$('#delete_button_wrapper').toggle();
	
	$('#cp_settings_button_line > a').blur();
	$('#cp_settings').slideToggle('normal');
	
	if (!b_treeLoaded) {
		cpSettingsLoadTree(cpDirId);
	}
}

function cpSettingsToggleLinks(val) {
	if (val == 'link' && $('#block_module_links').css('display') == 'none') {
		$('#block_module_links').slideDown('normal');
	}
	else if ($('#block_module_links').css('display') == 'block') {
		$('#block_module_links').slideUp('normal');
	}
}

function cpSettingsLoadTree(cpDirId) {
	$.ajax({
		url: baseURI+'/admin/cp/ajax_get_tree/',
		dataType: 'xml',
		type: 'get',
		error: function() { alert ('Ошибка при загрузке дерева элементов рабочего стола!')  },
		success: function(xml) {
			$(xml).find('data').find('response').find('object').each(function() {
				var option = $('<option>')
					.attr('value', $(this).attr('id'))
					.text(strRepeat('--', $(this).attr('level')) + ' ' + $(this).find('property[name=title]').find('value').text());
				
				if ($(this).attr('id') == cpDirId) {
					option.attr('selected', 'selected');
				}
				
				$('#cp_items_tree').append(option);
			});
		}
	});
	
	b_treeLoaded = true;
}

function cpSettingsError(message) {
	$('#cp_errors').append($('<div>').attr('class','system_error').text(message));
}

function cpSettingsErrorClear() {
	$('#cp_errors').empty();
}